/**
 * 注册点击事件
**/
var sms_timer = null;
var remain_time = 60;

$(document).ready(function(){
    // username login_pwd login_pwd_repeat deal_pwd deal_pwd_repeat captcha
    $("#confirm").click(function(){
        var param = getInputParam("form");
        console.log("param", param);
        // // 以下可以进行各种校验, 我做了手机号校验, 与二次密码相同校验.
        if (checkHasEmpty(param)) {
           return notify("请完整填写表单信息"); 
        }
        if (param.new_pwd !== param.new_pwd_repeat) {
            return notify("二次密码不一至"); 
        }

       
        
        // TODO 发送注册请求成功后跳转.
        confirm(param);
    });


     $("#vcode").click(function() {
        var param = getInputParam('form');
        if (remain_time !== 60) {
            return notify(remain_time + '秒后重新获取');
        }
        getSmsCode(param.username);
        // remain_time -= 1;

    });
});

function changeRemainTime() {
    sms_timer = setInterval(function() {
        remain_time--;
        if (!remain_time) {
            clearInterval(sms_timer);
            remain_time = 60;
            $("#vcode").html('获取验证码');
            return 0;
        }
        $("#vcode").html(remain_time + '秒');
     }, 1000)
}

function confirm(data) {
    $.ajax({
        type: "POST",//方法类型
        dataType: "json",//预期服务器返回的数据类型
        url: "/index.php/Home/Info/safepwd/" ,//url
        data:data,
        success: function (result) {
            if (result.code == 1) {
                notify(result.msg);
                window.location.href='/index.php/Home/Info/myziliao/';
            }else{
                notify(result.msg);
            }
        },
        error : function() {
            notify("异常！");
        }
    })
}

function getSmsCode() {
    $.ajax({
        type: "POST",//方法类型
        dataType: "json",//预期服务器返回的数据类型
        url: "/index.php/Home/Ajax/safemsg" ,//url
        data:'wwww',
        success: function (result) {
            notify("发送成功");
            changeRemainTime();
        },
        error : function() {
            notify("异常！");
            changeRemainTime();
        }
    })
}